package onion.molingo2o.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import onion.molingo2o.R;
import onion.molingo2o.adapter.OnionAdapter;
import onion.molingo2o.bean.SpeakTemp;
import onion.molingo2o.utils.PreferencesUtils;

/**
 * Created by Administrator on 2015/10/29.
 */
public class MyTempWindow extends PopupWindow {
    private GridView gridView;
    OnionAdapter adapter;
    public MyTempWindow(final Activity context,final TextView btn,final EditText input) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gridView = (GridView) inflater.inflate(R.layout.my_temp_window, null);
     final   ArrayList<SpeakTemp> list= PreferencesUtils.getTemps();
        adapter=new OnionAdapter(context,R.layout.item_temp,list,new String[]{"title","content"});
        gridView.setAdapter(adapter);
        // 设置按钮监听
        // 设置SelectPicPopupWindow的View
        this.setContentView(gridView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
//        Drawable drawable = context.getResources().getDrawable(R.drawable.operate_bg);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAsDropDown(btn);
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                input.setText(list.get(position).content);
                dismiss();
            }
        });
    }

}
